<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          :preIcon="IconEnum.UPLOAD"
          type="primary"
          @click="handleImport"
        >
          导入
        </a-button>
        <a-button
          :preIcon="IconEnum.DELETE"
          @click="handleDelete"
          type="primary"
          color="error"
        >
          删除
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: IconEnum.PREVIEW,
              tooltip: '预览',
              onClick: handlePreview.bind(null, record),
            },
            {
              icon: IconEnum.EDIT,
              tooltip: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: IconEnum.RESET,
              tooltip: '同步',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: IconEnum.DOWNLOAD,
              tooltip: '下载',
              onClick: handleDownload.bind(null, record),
            },
            {
              icon: IconEnum.EXPORT,
              tooltip: '生成代码',
              onClick: handleGenerate.bind(null, record),
            },
            {
              icon: IconEnum.DELETE,
              tooltip: '删除',
              color: 'error',
              onClick: handleDelete.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <GenModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive } from 'vue'
import GenModal from './GenModal.vue'
import { columns, searchFormSchema } from './gen.data'
import { useModal } from '@/components/Modal'
import { useMessage } from '@/hooks/web/useMessage'
import { useGo } from '@/hooks/web/usePage'
import {
  listGenApi,
  downloadGenApi,
  generateGenApi,
  delForceGenApi,
} from '@service/gen/generate/gen'
import { BasicTable, TableAction, useTable } from '@/components/Table'
import { IconEnum } from '@admin/tokens'
import { GenCodeDetailGo, GenGenerateDetailGo } from '@enums/gen'

export default defineComponent({
  name: 'GenManagement',
  components: { BasicTable, GenModal, TableAction },
  setup() {
    const state = reactive<{
      ids: (string | number)[]
      idNames: string
    }>({
      ids: [],
      idNames: '',
    })

    const go = useGo()
    const { createMessage, createConfirm } = useMessage()
    const [registerModal, { openModal }] = useModal()
    const [registerTable, { reload }] = useTable({
      title: '代码生成列表',
      api: listGenApi,
      columns,
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
      },
      striped: false,
      useSearchForm: true,
      showTableSetting: true,
      tableSetting: {
        fullScreen: true,
      },
      rowKey: 'id',
      bordered: true,
      showIndexColumn: true,
      actionColumn: {
        width: 220,
        title: '操作',
        dataIndex: 'action',
        slots: { customRender: 'action' },
        fixed: undefined,
      },
      rowSelection: {
        onChange: (selectedRowKeys, selectRows) => {
          state.ids = selectedRowKeys
          state.idNames = selectRows
            .map((item) => {
              return item.comment
            })
            .join(',')
        },
      },
    })

    /** 预览按钮 */
    function handlePreview(record: Recordable) {
      go(GenCodeDetailGo + record.id)
    }

    /** 导入按钮 */
    function handleImport() {
      openModal(true)
    }

    /** 修改按钮 */
    function handleEdit(record: Recordable) {
      go(GenGenerateDetailGo + record.id)
    }

    /** 下载按钮 */
    function handleDownload(record: Recordable) {
      downloadGenApi(record.id, 'xueyi.zip')
        .then(() => createMessage.success('下载成功！'))
        .catch(() => createMessage.warning('下载异常，请联系管理员！'))
    }

    /** 生成代码按钮 */
    function handleGenerate(record: Recordable) {
      generateGenApi(record.id)
        .then(() => createMessage.success('生成成功！'))
        .catch(() => createMessage.warning('生成异常，请联系管理员！'))
    }

    /** 删除按钮 */
    function handleDelete(record: Recordable) {
      const delIds = record.id || state.ids
      const delNames = record.name || state.idNames
      if (!record.id && state.ids.length === 0) {
        createMessage.warning('请选择要操作的数据！')
      } else {
        createConfirm({
          iconType: 'warning',
          title: 'Tip',
          content: '是否确定要删除' + delNames + '?',
          onOk: () =>
            delForceGenApi(delIds).then(() => {
              createMessage.success('删除' + delNames + '成功！')
              reload()
            }),
        })
      }
    }

    function handleSuccess() {
      reload()
    }

    return {
      registerTable,
      registerModal,
      handleImport,
      handlePreview,
      handleEdit,
      handleDownload,
      handleGenerate,
      handleDelete,
      handleSuccess,
      IconEnum,
    }
  },
})
</script>
